package com.tanhua.task;

import cn.hutool.core.date.DateUtil;
import com.tanhua.dao.AnalysisByDayMapper;
import com.tanhua.dao.LogMapper;
import com.tanhua.domain.AnalysisByDay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;


/*
*  springTask定时框架的使用：
*           1 在启动类上开启任务调用注解  @EnableScheduling
*           2 编写类和方法
*                 类：交给ioc容器
*                 方法：无参 无返回值 方法名任务
*                   设置方法的执行时间 注解：@Scheduled(cron=七子表达式）
*
* */


@Component
public class MyTask {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Scheduled(cron = "00 50 14 * * ?") //代表：每隔5秒钟 执行aaa方法
    public void aaa(){

        String time = DateUtil.format(new Date(), "yyyy-MM-dd");
        System.out.println(time);
        //查询日志--获取今日登录用户
        Integer numLogin=logMapper.findNumLogin(time,"0101");
       //查询日志--获取今日注册用户
        Integer numRegistered=logMapper.findNumLogin(time,"0102");
       //查询日志--获取今日活跃用户
        Integer numActive=logMapper.findNumActive(time);
       //查询日志--获取今日留存用户
        String yesterday = DateUtil.yesterday().toString("yyyy-MM-dd");
        Integer numRetention1d=logMapper.findNumRetention1d(yesterday,time);
       //将查到的数据insert到统计表中（tb_analysis_by_day）

        AnalysisByDay analysisByDay = new AnalysisByDay();
        analysisByDay.setNumRegistered(numRegistered);
        analysisByDay.setNumLogin(numLogin);
        analysisByDay.setNumActive(numActive);
        analysisByDay.setNumRetention1d(numRetention1d);
        analysisByDay.setRecordDate(DateUtil.parseDate(time));


        analysisByDay.setCreated(new Date());
        analysisByDay.setUpdated(new Date());
        System.out.println("统计的数据展示："+analysisByDay);
        analysisByDayMapper.insert(analysisByDay);

    }

}
